package com.huichenghe.xinlvsh01.Utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 截屏帮助类
 * Created by lixiaoning on 15-12-7.
 */
public class ShotScreenForShare
{
    private Activity mActivity;

    public ShotScreenForShare(Activity activity)
    {
        this.mActivity = activity;
    }


    private Bitmap takeScreenShot(Activity activity)
    {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();

        Bitmap b = view.getDrawingCache();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int stateBarHeight = rect.top;

        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        Bitmap bitmap = Bitmap.createBitmap(b, 0, stateBarHeight, width, height - stateBarHeight);
        view.destroyDrawingCache();
        return bitmap;

    }


    /**
     * 保存图片
     * @param bitmap
     * @param fileName
     */
    private void savePic(Bitmap bitmap, String fileName)
    {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(fileName);
            if(fileOutputStream != null)
            {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

                fileOutputStream.flush();
                fileOutputStream.close();

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public void takeshotScreen(Activity a)
    {


        savePic(takeScreenShot(a), "/sdcard/抽样.png");

    }





}
